package org.example.spring_book.service;

import org.example.spring_book.enums.BookStateEnums;
import org.example.spring_book.mapper.BookInfoMapper;
import org.example.spring_book.model.BookInfo;
import org.example.spring_book.model.PageRequest;
import org.example.spring_book.model.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ClassName BookService
 * @Description TODO
 * @Author wangxiaodong
 */
@Service
public class BookService {
    @Autowired
    private BookInfoMapper bookInfoMapper;
    public Integer insertBook(BookInfo bookInfo){
        return bookInfoMapper.insertBook(bookInfo);
    }

    public PageResult<BookInfo> getListByPage(PageRequest pageRequest){
        //1.查询记录的总数
        Integer count=bookInfoMapper.count();
        //2.查询当前页的数据
        List<BookInfo> bookInfos=bookInfoMapper.queryListByPage(pageRequest);
        for(BookInfo bookInfo:bookInfos) {
            bookInfo.setStateCN(BookStateEnums.getDescByCode(bookInfo.getStates()).getDesc());
        }
        return new PageResult<>(bookInfos,count,pageRequest);
   }


    public BookInfo queryBookById(Integer bookId) {
        return bookInfoMapper.queryBookById(bookId);
    }

    public Integer updateBook(BookInfo bookInfo) {
         return bookInfoMapper.updateBook(bookInfo);
    }

    public Integer batchDelete(List<Integer> ids) {
        return bookInfoMapper.batchDelete(ids);
    }
}
